<?php
class tplController extends RPHP_Controller
{

    //权限前缀
    protected $_pvprefix = "tpl";

/*----首页模板内容--------------*/

    //版块列表
    public function home()
    {

        $priv = array($this->_pvprefix . "_hm");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;

            //获取版块数据
            $this->view->data = $model->getHomeTpl();
            $this->view->ads  = $model->getHomeAds();

            $this->display();

        }

    }

    //版块排序
    public function homesort()
    {

        $priv = array($this->_pvprefix . "_hm");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;

            //获取版块数据
            $this->view->data = $model->getHomeTpl();

            $this->display();

        }

    }

    //新增版块
    public function homeadd()
    {

        $priv = array($this->_pvprefix . "_hm");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;

            $this->display();

        }

    }

    //编辑版块
    public function homeedit()
    {

        $priv = array($this->_pvprefix . "_hm");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;

            $htid = intval($_GET['htid']);
            $data = $model->getHomeTpl($htid);

            if ($data) {

                $this->view->data = $data;
                $this->display();
            } else {
                RPHP_RR::nodata();
            }

        }

    }

    //版块操作
    public function homedo()
    {

        $priv = array($this->_pvprefix . "_hm");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;
            $this->view->code = 900;

            if ($_POST['act'] == "add") {

                $this->view->success = "新增版块成功";
                $this->view->link    = array(
                    array('name' => '继续新增版块', 'url' => '/tpl/homeadd'),
                    array('name' => '返回首页内容', 'url' => '/tpl/home'),
                );
                $this->view->code = $model->saveHomeTpl($_POST);

            } elseif ($_POST['act'] == "edit") {

                $this->view->success = "编辑版块成功";
                $this->view->link    = array(
                    array('name' => '返回首页内容', 'url' => '/tpl/home'),
                );
                $this->view->code = $model->saveHomeTpl($_POST, $_POST['htid']);

            } elseif ($_POST['act'] == "delete") {
                $this->view->success = "删除版块成功";
                $this->view->link    = array(
                    array('name' => '返回首页内容', 'url' => '/tpl/home'),
                );
                $this->view->code = $model->deleteHomeTpl($_POST['dataid']);

            } elseif ($_POST['act'] == "sort") {

                $this->view->success = "保存排序成功";
                $this->view->link    = array(
                    array('name' => '返回首页内容', 'url' => '/tpl/home'),
                );
                $this->view->code = $model->saveHomeTplSort($_POST);

            }

            $this->displaydo();

        }

    }

/*--首页广告管理--*/

    //新增
    public function homead_add()
    {

        $priv = array($this->_pvprefix . "_hm");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;
            $htid             = intval($_GET['htid']);
            $data             = $model->getHomeTpl($htid);

            if ($data) {

                $this->view->data = $data;
                $this->display();
            } else {
                RPHP_RR::nodata();
            }

        }

    }

    //编辑
    public function homead_edit()
    {
        $priv = array($this->_pvprefix . "_hm");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;
            $htid             = intval($_GET['htid']);
            $ht               = $model->getHomeTpl($htid);
            if ($ht) {

                $adid = intval($_GET['adid']);
                $data = $model->getSingleHomeAd($adid);

                if ($data['adid'] > 0) {

                    if ($data['docid'] > 0) {
                        $data['docs'] = $model->getDocs($data['docid']);
                    }

                    $this->view->ht   = $ht;
                    $this->view->data = $data;

                    $this->display();

                } else {
                    RPHP_RR::nodata();
                }

            } else {
                RPHP_RR::nodata();
            }

        }
    }

    //排序
    public function homead_sort()
    {
        $priv = array($this->_pvprefix . "_hm");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;
            $htid             = intval($_GET['htid']);
            $ht               = $model->getHomeTpl($htid);
            if ($ht) {

                $data = $model->getHomeAds($ht['htid']);

                if ($data) {

                    $this->view->ht   = $ht;
                    $this->view->data = $data;

                    $this->display();

                } else {
                    $this->redirect("/tpl/home");
                }

            } else {
                RPHP_RR::nodata();
            }

        }
    }

    //图文信息操作
    public function savehomead()
    {
        $code = 900;

        $priv = array($this->_pvprefix . "_hm");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;

            //新增文本信息
            if ($_POST['act'] == "add") {

                $this->view->success = "新增广告成功";
                $this->view->link    = array(
                    array('name' => '继续新增广告', 'url' => '/tpl/homead_add?htid=' . $_POST['htid']),
                    array('name' => '返回首页内容', 'url' => '/tpl/home'),
                );

                $code = $model->saveHomeAd($_POST, $_FILES);
                //echo $code;
            }
            //编辑文本信息
            elseif ($_POST['act'] == "edit") {

                $this->view->success = "编辑广告成功";
                $this->view->link    = array(
                    array('name' => '返回首页内容', 'url' => '/tpl/home'),
                );

                $code = $model->saveHomeAd($_POST, $_FILES, $_POST['adid']);
            } elseif ($_POST['act'] == "delete") {

                $this->view->success = "删除广告成功";
                $this->view->link    = array(
                    array('name' => '返回首页内容', 'url' => '/tpl/home'),
                );
                $code = $model->deleteHomeAd($_POST['dataid']);

            }
            //保存排序
            elseif ($_POST['act'] == "sort") {

                $this->view->success = "保存排序成功";
                $this->view->link    = array(
                    array('name' => '返回首页内容', 'url' => '/tpl/home'),
                );

                $code = $model->saveHomeAdSort($_POST);
            }
        }

        $this->view->code = $code;

        $this->displaydo();
    }
}
